//
//  LoginViewController.m
//  Korea
//
//  Created by 杨东奇 on 15/9/1.
//  Copyright (c) 2015年 KoreaGo. All rights reserved.
//

#import "LoginViewController.h"
#import "AFNTool.h"
#import "RigsterViewController.h"
#import "ForgetViewController.h"
#import "AppDelegate.h"
#import "Header.h"
#import "UMSocial.h"

#define BASEHEIGHT self.view.frame.size.height / 33
#define BASEWIDTH self.view.frame.size.width / 19
@interface LoginViewController ()<UITextFieldDelegate>
@property (nonatomic, strong) UITextField *userField;
@property (nonatomic, strong) UITextField *passField;
@property (nonatomic, strong) MBProgressHUD *HUD;
@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.translucent = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    self.view.userInteractionEnabled = YES;
    [self creatLogInView];
    self.view.userInteractionEnabled = YES;
    

}

- (void) creatLogInView{
    
    // 取消按钮
    UIButton *cancel = [UIButton buttonWithType:UIButtonTypeSystem];
    cancel.frame = CGRectMake(BASEWIDTH * 32.7 / 2, BASEHEIGHT * 3.95 /2
                              , BASEWIDTH * 1.95 / 2, BASEHEIGHT * 2 / 2);
    [cancel setBackgroundImage:[UIImage imageNamed:@"cancel@2x.png"] forState:UIControlStateNormal];
    [cancel addTarget:self action:@selector(cancel) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:cancel];
    
    // 图标
    UIImageView *logo = [[UIImageView alloc] initWithFrame:CGRectMake(BASEWIDTH * 6.875, BASEHEIGHT * 2.925, BASEWIDTH * 5, BASEWIDTH * 5)];
    logo.backgroundColor = [UIColor colorWithRGB:0xf15b6c alpha:1];
    logo.image = [UIImage imageNamed:@""];
    [self.view addSubview:logo];
    
    // 用户名
    _userField = [[UITextField alloc] initWithFrame:CGRectMake(BASEWIDTH * 3,BASEHEIGHT * 9.175, BASEWIDTH* 12.75, BASEHEIGHT + 3)];
    _userField.placeholder = @"手机号";
    _userField.keyboardType = UIKeyboardTypeEmailAddress;
    _userField.returnKeyType = UIReturnKeyNext;
    _userField.delegate = self;
    _userField.clearButtonMode = UITextFieldViewModeWhileEditing;
    [self.userField setTextColor:[UIColor grayColor]];
    _userField.autocapitalizationType = UITextAutocapitalizationTypeNone;
    
    [self.view addSubview:self.userField];
    
    // 分割线
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetMinX(self.userField.frame), CGRectGetMaxY(self.userField.frame) + 5, CGRectGetWidth(self.userField.frame), 1)];
    view.backgroundColor = [UIColor colorWithRGB:0xcccbcc alpha:1];
    [self.view addSubview:view];
    
    // 密码
    self.passField = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMinX(self.userField.frame), CGRectGetMaxY(view.frame) + 15, CGRectGetWidth(self.userField.frame), CGRectGetHeight(self.userField.frame))];
    _passField.placeholder = @"密码";
    _passField.textColor = [UIColor grayColor];
    _passField.keyboardType = UIKeyboardTypeDefault;
    _passField.borderStyle = UITextBorderStyleNone;
    _passField.autocapitalizationType = UITextAutocapitalizationTypeNone;
    _passField.returnKeyType = UIReturnKeyDone;
    _passField.clearButtonMode = UITextFieldViewModeWhileEditing;
    _passField.secureTextEntry = YES;
    _passField.delegate = self;
    [self.view addSubview:self.passField];
    
    
    // 分割线
    UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetMinX(self.passField.frame), CGRectGetMaxY(self.passField.frame) + 5, CGRectGetWidth(self.passField.frame), 1)];
    view2.backgroundColor = [UIColor colorWithRGB:0xcccbcc alpha:1];
    [self.view addSubview:view2];
    
    // 忘记密码
    UIButton *foget = [UIButton buttonWithType:UIButtonTypeSystem];
    foget.frame = CGRectMake(CGRectGetMaxX(self.passField.frame) - BASEWIDTH * 4, CGRectGetMaxY(self.passField.frame) + 15, BASEWIDTH * 4, BASEHEIGHT + 3);
    [foget setTitle:@"忘记密码" forState:UIControlStateNormal];
    foget.tintColor = [UIColor colorWithRGB:0xf15b6c alpha:1];
    [foget addTarget:self action:@selector(foget) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:foget];
    
    // 登录
    UIButton *login = [UIButton buttonWithType:UIButtonTypeSystem];
    login.frame = CGRectMake(BASEWIDTH * 1.5, BASEHEIGHT * 17.6, BASEWIDTH * 15.75, BASEHEIGHT * 2);
    [login setTitle:@"登    录" forState:UIControlStateNormal];
    [login addTarget:self action:@selector(logIn:) forControlEvents:UIControlEventTouchUpInside];
    login.tintColor = [UIColor whiteColor];
    [login setBackgroundImage:[UIImage imageNamed:@"button@2x.png"] forState:UIControlStateNormal];
    
    [self.view addSubview:login];
    
    // 注册
    UIButton *rigster = [UIButton buttonWithType:UIButtonTypeSystem];
    rigster.frame = CGRectMake(BASEWIDTH * 1.5, CGRectGetMaxY(login.frame) + 15, BASEWIDTH * 15.75, BASEHEIGHT * 2);
    [rigster setTitle:@"注    册" forState:UIControlStateNormal];
    [rigster addTarget:self action:@selector(rigster:) forControlEvents:UIControlEventTouchUpInside];
    rigster.tintColor = [UIColor whiteColor];
    [rigster setBackgroundImage:[UIImage imageNamed:@"button@2x.png"] forState:UIControlStateNormal];
    
    [self.view addSubview:rigster];
    
    // 分割线
    UIImageView *view3 = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetMinX(rigster.frame) - 10, CGRectGetMaxY(rigster.frame) + 50, CGRectGetWidth(rigster.frame), 11)];
    view3.image = [UIImage imageNamed:@"or@2x.png"];
    [self.view addSubview:view3];
    
    // qq登录
    UIButton *qq = [UIButton buttonWithType:UIButtonTypeSystem];
    qq.frame = CGRectMake(BASEWIDTH * 3.35 , CGRectGetMaxY(view3.frame) + BASEHEIGHT * 1.325, BASEWIDTH * 2.075, BASEHEIGHT * 2.775);
    [qq setBackgroundImage:[UIImage imageNamed:@"qq@2x.png"] forState:UIControlStateNormal];
    [qq addTarget:self action:@selector(qq) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:qq];
    
    // 微信登录
    UIButton *wx = [UIButton buttonWithType:UIButtonTypeSystem];
    wx.frame = CGRectMake(CGRectGetMaxX(qq.frame) + BASEWIDTH * 2.875, CGRectGetMaxY(view3.frame) + BASEHEIGHT * 1.32, BASEWIDTH * 2.075, BASEHEIGHT * 2.775);
    [wx setBackgroundImage:[UIImage imageNamed:@"微信@2x.png"] forState:UIControlStateNormal];
    [wx addTarget:self action:@selector(wx) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:wx];
    
    // 微博登录
    UIButton *wb = [UIButton buttonWithType:UIButtonTypeSystem];
    wb.frame = CGRectMake(CGRectGetMaxX(wx.frame) + BASEWIDTH * 2.875, CGRectGetMaxY(view3.frame) + BASEHEIGHT * 1.32, BASEWIDTH * 2.075, BASEHEIGHT * 2.775);
    [wb setBackgroundImage:[UIImage imageNamed:@"新浪微博@2x.png"] forState:UIControlStateNormal];
    [wb addTarget:self action:@selector(wb) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:wb];
    
}
- (void)wx{
    
}
- (void)wb{
    UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToSina];
    
    snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
        
        //          获取微博用户名、uid、token等
        
        if (response.responseCode == UMSResponseCodeSuccess) {
            
            UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:UMShareToSina];
            
            NSLog(@"username is %@, uid is %@, token is %@ url is %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL);
            
        }});
    
}
- (void)qq{
    NSLog(@"qq");
}
- (void)foget{

    ForgetViewController *forgetVC = [[ForgetViewController alloc] init];
    [self.navigationController pushViewController:forgetVC animated:YES];

}
- (void)cancel{
    [self dismissViewControllerAnimated:YES completion:nil];
    
//    [self dismissViewControllerAnimated:YES completion:nil];
    [self.view removeFromSuperview]
    ;
//    UIWindow *window = [UIApplication sharedApplication].keyWindow;
//    AppDelegate *app = [[AppDelegate alloc] init];
//    window.rootViewController = app.KoreaTabBarController;
    
}

- (void)logIn:(UIButton *)but{
    NSString *log = @"http://192.168.0.8:8083/user/login";
    //1.管理器
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    //2.设置登录参数
    NSDictionary *dict = @{ @"u":self.userField.text, @"p":self.passField.text};
    
    //3.请求
    [manager POST:log parameters:dict success: ^(AFHTTPRequestOperation *operation, id responseObject) {
        
        if ([responseObject[@"result"] isEqualToString:@"用户名或密码错误"]) {
            self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            self.HUD.mode = MBProgressHUDModeText;
            self.HUD.color = [UIColor blackColor];
            self.HUD.alpha = 0.5;
            self.HUD.minSize = CGSizeMake(BASEWIDTH * 9, BASEWIDTH * 6);
            self.HUD.labelText = @"用户名或密码错误";
            self.HUD.dimBackground = YES;
            
        }else{
            NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
            NSString *token = responseObject[@"result"];
            [user setValue:token forKey:@"userInfo"];
            [self dismissViewControllerAnimated:YES completion:nil];
            self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            self.HUD.mode = MBProgressHUDModeText;
            self.HUD.color = [UIColor blackColor];
            self.HUD.alpha = 0.5;
            self.HUD.minSize = CGSizeMake(BASEWIDTH * 9, BASEWIDTH * 6);
            self.HUD.labelText = @"登录成功";
            self.HUD.dimBackground = YES;
            self.HUD.minShowTime = 1.0;
            
            UIWindow *window = [UIApplication sharedApplication].keyWindow;
            AppDelegate *app = [[AppDelegate alloc] init];
            window.rootViewController = app.KoreaTabBarController;
            
            
        }
        
    } failure: ^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@", error);
    }];
    
}

- (void)rigster:(UIButton *)but{
    
    

    
    RigsterViewController *rVC = [[RigsterViewController alloc] init];
    
    
    
    [self.navigationController pushViewController:rVC animated:YES];
    
    
    
    
    
    
    
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    // 点击Done隐藏键盘
    if (textField == self.passField) {
        [textField resignFirstResponder];
    } else
    {
        // 点击next跳到下一个输入框
        [self.userField resignFirstResponder];
        [self.passField becomeFirstResponder];
        
    }
    return YES;
    
}
// 点击屏幕隐藏键盘
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self.userField resignFirstResponder];
    [self.passField resignFirstResponder];
    [self.HUD hide:YES];
    [self.HUD removeFromSuperview];
}

- (void)viewWillAppear:(BOOL)animated{

        self.navigationController.navigationBarHidden = YES;
 
   }
- (void)viewWillDisappear:(BOOL)animated{
    self.navigationController.navigationBarHidden = NO;
}


@end
